<?php
	
	require_once ("../src/egallery/config/mysql_config.inc.php") ;
	require_once ("../src/egallery/net/mysql/MysqlConnector.php") ;
	require_once ("../src/egallery/vo/PictureVO.php") ;

	/**
	 * This AMFPhp remoting service is used to return the list of all pictures in a gallery.
	 * @author eKameleon
	 */
	class Gallery
	{
	   
	    /**
		 * Creates a new Gallery instance.
		 */
		function Gallery() 
		{
			
			$this->connect = new MysqlConnector( _HOST_  , _USER_ , _PASS_ , _NAME_ ) ;
					
			$this->methodTable = array 
			(
			    
			    "getGallery" => array
			    (
			        "description" => "generate a list of pictures" ,
	            	"access"      => "remote"
			    )
			    ,
				"_getPicture" => array
			    (
	            	"description" => "Return a PictureVO." ,
	            	"access"      => "private"
			    )
			    ,
				"_getResult" => array
			    (
	            	"description" => "Return the result of a MYSQL query." ,
	            	"access"      => "private"
			    )
	
	        ) ;
	    } 

	    /**
	 	 * The internal MysqlConnector instance.
		 */
		var $connect ;
		
		/**
		 * The method table use by AMFPHP.
		 */
		var $methodTable ;
		
		/**
		 * This method return the list of all PictureVO in the db.
		 */
		function getGallery()
		{	
			
			$stack = array() ;
			
			$query = "SELECT * FROM ". _TABLE_GALLERY_ ;
			
			$result = $this->_getResult( $query ) ;
			
			if (mysql_num_rows($result) > 0)
			{
				while( $row = mysql_fetch_array( $result ) )
				{	
					
					$pic = $this->_getPicture( $row ) ;
					array_push( $stack, $pic ) ;
					
				}
			}
			
			return $stack ;
			
		}
	   	   
		/**
		 * Returns a new PictureVO specified by the row value in argument.
		 * @return a new PictureVO specified by the row value in argument.
		 */
		function _getPicture( $row )
		{
								
			$pic = new PictureVO() ;
					
			$pic->id      		= $row["id"] ;
			$pic->description 	= utf8_encode( $row["description"] ) ;
			$pic->title   		= utf8_encode( $row["title"] ) ;
			$pic->url			= $row["url"] ;
												
			return $pic ;
			
		}
		
		/**
		 * Returns the result of a query.
		 * @return the result of a query.
		 */
	    function _getResult( $query )
		{
		   return $this->connect->getResult($query) ; 
		}
		
	}
?>
